package com.hc.serviceImpl;

import java.util.Iterator;
import java.util.List;

import com.hc.bean.BSizeBean;
import com.hc.comm.CommData;
import com.hc.service.GradeConfigService;
import com.hc.util.BSizeConfigUtil;

public class GradeConfigServiceImpl implements GradeConfigService{

	@Override
	public boolean updateConfig(BSizeBean updateBean) {
		
		synchronized (CommData.BSizeBeans) {
			Iterator<BSizeBean> bean = CommData.BSizeBeans.iterator();
			
			while (bean.hasNext()) { 
				BSizeBean  entry =  bean.next(); 
					
				if(entry.getId()==updateBean.getId()){
					
					entry.setName(updateBean.getName());
					entry.setMaxSpeed(updateBean.getMaxSpeed());
					entry.setMinSpeed(updateBean.getMinSpeed());
					saveConfig();
					return true;
					
				}
				
			} 
		}
		

		return false;
	}

	private void saveConfig() {
		BSizeConfigUtil.writeConfig(CommData.BSizeBeans);
		
	}

	@Override
	public boolean addConfig(BSizeBean bean) {

		CommData.BSizeBeans.add(bean);
		
		saveConfig();
		return true;
	}

	@Override
	public boolean delConfigBean(long id) {
		
		Iterator<BSizeBean> bean = CommData.BSizeBeans.iterator();
	
		while (bean.hasNext()) { 
			BSizeBean  entry =  bean.next(); 
				
			if(entry.getId()==id){
				
				bean.remove();
				saveConfig();
				return true;
			}
			
		} 
		return false;
	}

	@Override
	public List<BSizeBean> getConfigs() {
	
		return CommData.BSizeBeans;
	}

}
